package org.zhouhe.modules.servingProcess.service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Service;
import org.zhouhe.base.BaseService;
import org.zhouhe.vo.ServingProcess;

@Service
@SuppressWarnings("unchecked")
public class ServingProcessService extends BaseService {

	public void saveOrUpdate(ServingProcess sp) {
		if (sp.getServingProcessId() == null) {
			sp.setStatus(1);
			sp.setCreateTime(new Timestamp(new Date().getTime()));
			dao.save(sp);
		} else {
			ServingProcess servingProcess = (ServingProcess) dao.get(ServingProcess.class, sp.getServingProcessId());
			servingProcess.setContent(sp.getContent());
			servingProcess.setDisplayTime(sp.getDisplayTime());
			servingProcess.setTitle(sp.getTitle());
			servingProcess.setUpdateTime(sp.getUpdateTime());
			dao.update(servingProcess);
		}
	}

	public void delete(ServingProcess sp) {
		dao.delete(sp);
	}

	public ServingProcess getServingProcess(Integer servingProcessId) {
		return (ServingProcess) dao.get(ServingProcess.class, servingProcessId);
	}

	public List<ServingProcess> getServingProcessList() {
		return dao.find("from ServingProcess where status=1 order by orderId asc, servingProcessId asc");
	}

	public void updateDisplayOrder(List<ServingProcess> servingProcessList) {
		if(servingProcessList != null){
			for (ServingProcess sp : servingProcessList) {
				if(sp.getOrderId() == null){
					sp.setOrderId(0);
				}
				Query q = dao.getSessionFactory().getCurrentSession().createQuery("update ServingProcess set orderId=:orderId where servingProcessId=:id");
				q.setInteger("orderId", sp.getOrderId());
				q.setInteger("id", sp.getServingProcessId());
				q.executeUpdate();
			}
		}
	}
}
